package cn.sibu.myroot;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by 刘红亮 on 15-12-17 上午9:48.
 */
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback ,Runnable{

    private  SurfaceHolder surfaceHolder;
    private Paint paint;
    private  Thread th;
    private Canvas canvas;
    private boolean flag = true;
    //定义两个圆形的中心点坐标与半径
    private  float smallCenterX = 120,smallCenterY = 120, smallCenterR = 40;
    private  float BigCenterX = 120,BigCenterY = 120,BigCenterR=90;

    //当前圆周运动的角度
    private int angle;

    public MySurfaceView(Context context) {
        super(context);
        init(null,0);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, 0);
    }

    private void  init(AttributeSet attrs,int defStyle){

        surfaceHolder = this.getHolder();
        surfaceHolder.addCallback(this);
        paint = new Paint();
        paint.setColor(Color.WHITE);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
                th = new Thread(this);
        th.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    private  void myDraw(){
        canvas = surfaceHolder.lockCanvas();

        if(canvas!=null){
            //绘制大圆
            paint.setAlpha(0x77);
            canvas.drawRGB(0,0,0);//刷屏,有几种方式, canvas.drawColor(Color.BLACK),canvas.drawRect(0,0,this.getWidth(),this.getHeight(),paint);
            canvas.drawCircle(BigCenterX,BigCenterY,BigCenterR,paint);
            //绘制小圆

            canvas.drawCircle(smallCenterX,smallCenterY,smallCenterR,paint);
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
    /**
     * centerX  围绕的圆形(大圆)中心点x坐标
     *
     * enterY  围绕的圆形(大圆)中心点Y坐标
     *
     * R 围绕的圆形(大圆)半径
     *
     * rad 旋转的弧度
     * */
    public void setSmalCircleXY(float centerX,float centerY,float R,double rad){
        //获取圆周运动的x坐标
        smallCenterX = (float)(R*Math.cos(rad))+centerX;

        //获取圆周运动的Y坐标

        smallCenterY = (float)(R*Math.sin(rad)+centerY);

    }

    /**
     * 得到两点之间的弧度
     * px1 第一个点的x坐标
     * py1 第一个点的Y坐标
     * px2 第二个点的x坐标
     * py2 第二个点的y坐标
     */

    public double getRad(float px1, float py1,float px2,float py2){
        //得到两点x的距离
        float x = px2-px1;
        //得到两点y的距离
        float y = py1-py2;
        //算出斜边长

        float Hypotenuse = (float) Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
        //得到这个角度的余弦值(角度余弦值 = 邻边/斜边);
        float cosAngle = x/ Hypotenuse;
        //通过反余弦定理获取其角度的弧度
        float rad = (float) Math.acos(cosAngle);
        //当触屏的位置Y坐标<摇杆的Y坐标,取反值-0~180;
        if(py2<py1){
            rad = -rad;
        }
        return rad;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        Log.e("onTouchEvent","onTouchEvent");
        //当用户手指抬起,应该恢复小圆到初始位置
        if(event.getAction()==MotionEvent.ACTION_UP){
            smallCenterX = BigCenterX;
            smallCenterY = BigCenterY;
        }else {
            int pointX= (int) event.getX();
            int pointY = (int) event.getY();
            //判断用户点击的位置是否在大圆内
            if(Math.sqrt(Math.pow((BigCenterX-(int)event.getX()),2)+
            Math.pow((BigCenterY-(int)event.getY()),2))<=BigCenterR){
                //让小圆跟随用户触点位置移动
                smallCenterX = pointX;
                smallCenterY = pointY;
            }else {
                setSmalCircleXY(BigCenterX,BigCenterY,BigCenterR,
                        getRad(BigCenterX,BigCenterY,pointX,pointY));
            }
        }
        return true;
    }

    @Override
    protected boolean dispatchHoverEvent(MotionEvent event) {
        Log.e("dispatchHoverEvent","dispatchHoverEvent");
        return super.dispatchHoverEvent(event);
    }

    private void logic(){
        //让角度在0~360循环
        angle++;
        if(angle>=360){
            angle = 0;
        }
        //弧度 = 角*PI/180
        setSmalCircleXY(BigCenterX,BigCenterY,BigCenterR,angle*Math.PI/180);
    }



    @Override
    public void run() {

        while (flag){
            long start = System.currentTimeMillis();
            myDraw();
            logic();

            long end = System.currentTimeMillis();


                try {
                    if(end-start<50){
                    Thread.sleep(50 - (end - start));
                    }
                    } catch (InterruptedException e) {
                    e.printStackTrace();
                }

        }
    }
}
